<?php
//include MySQL DB code.
include ("../Action/config.php");

function sendGcmMessage($message) {
	//write php code.
	$gcmUrl = "https://android.googleapis.com/gcm/send";
	$serverKey = "AIzaSyDQgNx_VAMo17vSPIc6X9XrXuX-tNfXEtg";

	//for json
	$headers = array('Authorization:key=' . $serverKey, 'Content-Type: Application/json');

	//construct message to GCM Server.
	$sendData = array();
	$sendData['data'] = array();
	$sendData['data']['message'] = "Empty Message from DEOKIL";
	$sendData['registration_ids'] = array();

	//get json message sent from user
	$json = urldecode(file_get_contents("php://input"));
	$arrayJson = json_decode($json, true);
	$message = $arrayJson['message'];

	//set message to gcm data
	$sendData['data']['message'] = $message;

	//set reg_ids to gcm data
	$sql = "SELECT reg_id FROM CHILD";
	$result = mysql_query($sql);
	$i = 0;
	while ($row = mysql_fetch_array($result)) {
		$sendData['registration_ids'][$i++] = $row['reg_id'];
	};

	//php post
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendData));
	$response = curl_exec($ch);
	echo $response;
	curl_close($ch);
}

//write php code.
$gcmUrl = "https://android.googleapis.com/gcm/send";
$serverKey = "AIzaSyDQgNx_VAMo17vSPIc6X9XrXuX-tNfXEtg";

//for json
$headers = array('Authorization:key=' . $serverKey, 'Content-Type: Application/json');

//construct message to GCM Server.
$sendData = array();
$sendData['data'] = array();
$sendData['data']['message'] = "Empty Message from DEOKIL";
$sendData['registration_ids'] = array();

//get json message sent from user
$json = urldecode(file_get_contents("php://input"));
$arrayJson = json_decode($json, true);
$message = $arrayJson['message'];

//set message to gcm data
$sendData['data']['message'] = $message;

//set reg_ids to gcm data
$sql = "SELECT reg_id FROM CHILD";
$result = mysql_query($sql);
$i = 0;
while ($row = mysql_fetch_array($result)) {
	$sendData['registration_ids'][$i++] = $row['reg_id'];
};

//php post
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendData));
$response = curl_exec($ch);
echo $response;
curl_close($ch);
?>
